package com.appkubes.mall.admin.config.camel;

import com.appkubes.common.utils.data.string.CamelUtils;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class CamelFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ParameterRequestWrapper changeRequestWrapper = new ParameterRequestWrapper((HttpServletRequest) servletRequest);
        Map<String, Object> map = new HashMap<>();
        Map<String, String[]> maps = servletRequest.getParameterMap();
        for (String key : maps.keySet()) {
            map.put(CamelUtils.underlineToCamel(key), maps.get(key));
        }
        changeRequestWrapper.addAllParameters(map);
        filterChain.doFilter(changeRequestWrapper, servletResponse);
    }

    @Override
    public void destroy() {

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}
